package cn.gyouzhe.source.handlers;

import cn.gyouzhe.component.disruptor.AbstractHandler;
import cn.gyouzhe.entity.Coupon;
import cn.gyouzhe.service.ICouponService;
import cn.gyouzhe.utils.Common;
import cn.gyouzhe.source.vo.GoodVo;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 券信息保存
 *
 * @author wangchuan
 * @datetime 2018/2/11 23:30
 */
class CouponStoreHandler extends AbstractHandler {
    private ICouponService couponService;


    private ICouponService couponService() {
        if(null == couponService) {
            synchronized(this) {
                if(null == couponService) {
                    couponService = Common.getBean(ICouponService.class);
                }
            }
        }
        return couponService;
    }

    @Override
    public void handle(Object data) {
        GoodVo goodVo = (GoodVo) data;
        String couponId = goodVo.getCouponId();
        Coupon coupon = couponService().selectCoupon(couponId);
        if(null == coupon) {
            String goodId = goodVo.getGoodId();
            BigDecimal couponPrice = goodVo.getCouponPrice();
            BigDecimal couponCondition = goodVo.getCondition();
            LocalDateTime couponStartTime = goodVo.getCouponStartTime();
            LocalDateTime couponEndTime = goodVo.getCouponEndTime();
            coupon = Coupon.of(goodId, couponId, couponPrice, couponCondition, couponStartTime, couponEndTime);
            couponService.saveCoupon(coupon);
        }
        goodVo.setStoreCouponId(coupon.getId());

    }
}
